/**
 * 
 */
package com.revenco.bi.service.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.json.JSONArray;
import org.json.JSONObject;
import org.nutz.json.Json;
import org.nutz.lang.Lang;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.revenco.bi.dao.TnChartBaseinfoDao;
import com.revenco.bi.dao.TnTableInfoDao;
import com.revenco.bi.model.TnChartBaseinfo;
import com.revenco.bi.model.userbean.XFieldsBean;
import com.revenco.bi.service.TnChartBaseinfoService;
import com.revenco.bi.util.JsonBinder;

/**
 * @author LingJun
 * @version $Revision:1.0.0, 2014年1月21日 下午3:15:10
 */
@Service
public class TnChartBaseinfoServiceImpl implements TnChartBaseinfoService {
	@Autowired
	TnChartBaseinfoDao tnChartBaseinfoDao;
	@Autowired
	TnTableInfoDao tnTableInfoDao;

	public List<TnChartBaseinfo> getChartList() {
		return tnChartBaseinfoDao.getChartList();
	}

	public String save(String strJson, String chartType, String xFields,
			String sql, String mulRelationData) {
		TnChartBaseinfo baseInfo = new TnChartBaseinfo();
		String chartId = "chart" + new Date().getTime();
		baseInfo.setChartId(chartId);

		baseInfo = this.newModel(baseInfo, strJson, chartType, xFields, sql, mulRelationData);

		this.tnChartBaseinfoDao.saveChartBaseInfo(baseInfo);

		return chartId;
	}

	public void update(String strJson, String chartType, String xFields,
			String sql, String chartId, String mulRelationData) {
		TnChartBaseinfo baseInfo = this.getChartinfoById(chartId);

		baseInfo = this.newModel(baseInfo, strJson, chartType, xFields, sql, mulRelationData);

		this.tnChartBaseinfoDao.updateChartBaseInfo(baseInfo);
	}

	private TnChartBaseinfo newModel(TnChartBaseinfo baseInfo, String strJson,
			String chartType, String xFields, String sql, String mulRelationData) {
		@SuppressWarnings("rawtypes")
		Map mapTemp = new HashMap();
		mapTemp = Json.fromJson(HashMap.class, Lang.inr(strJson));

		JsonBinder binder = new JsonBinder();
		XFieldsBean xField = binder.fromJson(xFields, XFieldsBean.class); // x轴数据

		// 标题
		String chartTitle = mapTemp.get("chart_title").toString();
		baseInfo.setChartTitle(chartTitle);
		baseInfo.setChartType(chartType);
		baseInfo.setDataUnit(null);
		baseInfo.setxFieldName(xField.getcName());
		if ("circularGauge".equals(chartType)) {
			baseInfo.setxFieldTableName("0");
			baseInfo.setxFieldCd("0");
		} else {
			baseInfo.setxFieldTableName(xField.getTableName());
			baseInfo.setxFieldCd(xField.getName());
		}

		baseInfo.setColumnId(xField.getColumnId());
		baseInfo.setDrillPath(xField.getText()); // 设置 DrillPath
		
		// 图形属性
		baseInfo.setChartConfigJson(strJson);
		baseInfo.setChartDataSql(sql);
		// 标题显示字体大小
		String titleFontSize = mapTemp.get("title_font_size").toString();
		baseInfo.setChartTitleSize(titleFontSize);
		// 标题是否显示
		String titleEnabled = mapTemp.get("title_enabled").toString();
		baseInfo.setIfShowTitle(titleEnabled);
		// 图例是否显示
		String legendEnabled = mapTemp.get("legend_enabled").toString();
		baseInfo.setIfShowLegend(legendEnabled);
		// 图例显示位置
		String legendPosition = mapTemp.get("legend_position").toString();
		baseInfo.setLegendLocation(legendPosition);
		// 把设置关联关系的两张表，存入ext3字段中
		baseInfo.setExt3(getMulRelatTable(mulRelationData));
		baseInfo.setTableRelationJson(mulRelationData);
		return baseInfo;
	}

	public TnChartBaseinfo getChartinfoById(String chartId) {
		List<TnChartBaseinfo> list = tnChartBaseinfoDao
				.getChartinfoById(chartId);
		if (list != null && list.size() > 0) {
			return list.get(0);
		}
		return null;
	}
	
	public boolean deleteById(String chartId) {
		return this.tnChartBaseinfoDao.deleteById(chartId);
	}
	
	public String getMulRelatTable(String mulRelationData) {
		String mulRelatTable = "";
		if (!mulRelationData.equals("{}")) {
			JSONObject rootObj;
			rootObj = new JSONObject(mulRelationData);
			JSONArray array = rootObj.getJSONArray("COLUMN");
			String leftTable = "";
			String rightTable = "";
			for (int i = 0; i < array.length(); i++) {
				JSONObject temp = array.getJSONObject(i);
				leftTable = getTable((String) temp.get("first_Column")); // 前台传入的是table_id
				rightTable = getTable((String) temp.get("last_Column"));
			}
			mulRelatTable = tnTableInfoDao.getTableNameById(leftTable) + "," +
					tnTableInfoDao.getTableNameById(rightTable);
		}
		return mulRelatTable;
	}
	
	public String getTable(String json) {
		String table = json.split("=")[0];
		return table;
	}
}
